package com.mylib.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.mylib.libcore.R;
import com.mylib.libcore.utils.ActivityUtil;
import com.mylib.libcore.utils.UIUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Random;


/**
 * @author Administrator
 * @date 2019/12/20-9:38
 * @description CustomCoinView-自定义金币
 **/
public class CustomCoinView extends RelativeLayout {
    private Context mContext;
    private View mView;
    private RelativeLayout relativeLayout;
    //接口回调
    public ViewClink viewClink;
    public interface ViewClink {
        void itemClink(View view,String jsonString, String coinNumber);
    }
    public void SetViewClink(ViewClink viewClink){
        this.viewClink = viewClink;
    }

    public CustomCoinView(Context context) {
        this(context, null);
    }

    public CustomCoinView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomCoinView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }


    private void init(Context context, AttributeSet attrs) {
        mContext = context;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.custom_coinview_layout, this, true);
        relativeLayout = (RelativeLayout) mView.findViewById(R.id.rl_coin_view);
    }

    /**
     * 随机出现金币
     * coinNumber 金币数量
     */
    public void randomCustomCoinView( final int viewX, final int viewY,String title,String coin) {
        final View itemView =  View.inflate(mContext, R.layout.item_main_glod_coin_view,null);
        final TextView item1 = itemView.findViewById(R.id.tv_gold_num);
        final TextView item2 = itemView.findViewById(R.id.tv_gold_num_text);
        item1.setBackground(mContext.getResources().getDrawable(R.drawable.img_main_coin_gold));//设置图片
        item1.setTextColor(mContext.getResources().getColor(R.color.white));
        item1.setText(coin+"");
        item2.setText(title+"");
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        Random random = new Random();
        int locationY = random.nextInt(Math.abs(viewY-UIUtils.dp2px(65))+1);
        int locationX =random.nextInt( Math.abs(viewX/2-UIUtils.dp2px(6))+1);
        switch (random.nextInt(2)){
            case 0://左上对齐
                lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                lp.topMargin = locationY;
                lp.leftMargin =  locationX;
                break;
            case 1://右上对齐
                lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                lp.topMargin = locationY;
                lp.rightMargin =  locationX;
                break;
        }

        itemView.setLayoutParams(lp);//设置布局参数
        itemView.setOnClickListener(v -> {
            if(viewClink!= null){
                relativeLayout.removeView(itemView);
                viewClink.itemClink(itemView,"",coin);
            }
        });
        ActivityUtil.getInstance().setCoinViewUpDownAnim(itemView);
        relativeLayout.addView(itemView);//RelativeLayout添加子View
    }

    /**
     * 清除所有view
     */
    public void removeAllView(){
        relativeLayout.removeAllViews();//RelativeLayout添加子View
    }

}
